package com.adl.hotelmanage.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.adl.hotelmanage.dao.DbInquire;
import com.adl.hotelmanage.po.RoomInfo;
import com.google.gson.Gson;
import com.google.gson.JsonArray;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class AddRoomAjaxServlet
 */
@WebServlet("/AddRoomAjaxServlet")
public class AddRoomAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddRoomAjaxServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		List<RoomInfo> Pricels = new ArrayList<RoomInfo>();
		Map<String, String> datas = new HashMap<String, String>();
		String roomtype = request.getParameter("roomtype_typeName");
		String sql1 = "select * from t_roomtype where roomtype_typeName='" + roomtype + "'";
		ResultSet rs2 = DbInquire.inquire(sql1);
		String json = null;
		try {
			String sql2 = "select roomtype_price from t_roomtype where roomtype_typeName='" + roomtype + "'";
			String sql3 = "select roomtype_bedNum from t_roomtype where roomtype_typeName='" + roomtype + "'";
			ResultSet rs3 = DbInquire.inquire(sql2);
			ResultSet rs4 = DbInquire.inquire(sql3);
			while (rs3.next()&&rs4.next()) {
				System.out.println(sql2);
				System.out.println(sql3);
				RoomInfo roominfo1 = new RoomInfo();
				roominfo1.setPrice(Integer.parseInt(rs3.getString("roomtype_price")));
				roominfo1.setBednum(Integer.parseInt(rs4.getString("roomtype_bedNum")));
				Pricels.add(roominfo1);
				 Gson gson = new Gson();
				 json = gson.toJson(Pricels);
				 System.out.println("json="+ json);
				 request.setAttribute("listprice", Pricels);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		response.getWriter().print(json);
		response.getWriter().flush();
		response.getWriter().close();
	}

}
